<template>
  <div class="roles">
    <el-card>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple-dark">
            <el-button type="primary" size="default" @click="addShow = true"
              >添加角色</el-button
            >
          </div>
        </el-col>
      </el-row>
      <el-table :data="rolesList" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="propss">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-table
                :data="propss.row.children"
                border
                style="width: 100%"
                :show-header="false"
              >
                <el-table-column prop="id" label="ID" width="140">
                  <template slot-scope="props">
                    <el-tag closable @close="rmTagRoles(propss.row.id, props.row.id)">
                      {{ props.row.authName }}
                    </el-tag>
                    <i class="el-icon-caret-right"></i>
                  </template>
                </el-table-column>
                <el-table-column label="ID">
                  <template slot-scope="props">
                    <el-table
                      :data="props.row.children"
                      border
                      style="width: 100%"
                      :show-header="false"
                    >
                      <el-table-column>
                        <template slot-scope="props">
                          <el-tag
                            closable
                            type="success"
                            @close="rmTagRoles(propss.row.id, props.row.id)"
                          >
                            {{ props.row.authName }}
                          </el-tag>
                          <i class="el-icon-caret-right"></i>
                          <span v-for="(item, index) in props.row.children" :key="index">
                            <el-tag
                              closable
                              type="warning"
                              @close="rmTagRoles(propss.row.id, item.id)"
                            >
                              {{ item.authName }}
                            </el-tag>
                          </span>
                        </template>
                      </el-table-column>
                    </el-table>
                  </template>
                </el-table-column>
              </el-table>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="ID" type="index" width="60px"> </el-table-column>
        <el-table-column label="角色名称" prop="roleName"> </el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"> </el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="medium"
              icon="el-icon-edit"
              @click="getId(scope.row.id)"
            ></el-button
            ><!--查询需传值 -->
            <el-button
              type="danger"
              size="medium"
              icon="el-icon-delete"
              @click="reUsers(scope.row.id)"
            ></el-button>
            <el-button
              type="warning"
              size="medium"
              icon="el-icon-share"
              @click="lesShow(scope.row.children)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog title="添加角色" :visible.sync="addShow">
        <el-form :model="addForm" :rules="rules" ref="addForm">
          <el-form-item label="角色名称" label-width="100px" prop="roleName">
            <el-input v-model="addForm.roleName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色描述" label-width="100px" prop="roleDesc">
            <el-input v-model="addForm.roleDesc" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="addShow = false">取 消</el-button>
            <el-button type="primary" @click="addRoles('addForm')">确 定</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>

      <el-dialog title="修改角色" :visible.sync="upShow">
        <el-form :model="upForm" :rules="rules" ref="upForm">
          <el-form-item label="角色名称" label-width="100px" prop="roleName">
            <el-input v-model="upForm.roleName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色描述" label-width="100px" prop="roleDesc">
            <el-input v-model="upForm.roleDesc" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="rolesShow = false">取 消</el-button>
            <el-button type="primary" @click="addRoles">确 定</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>

      <el-dialog title="分配角色" :visible.sync="rolesShow">
        <el-tree
          :data="data"
          show-checkbox
          default-expand-all
          node-key="id"
          ref="tree"
          highlight-current
          :props="defaultProps"
        >
        </el-tree>
        <div class="el-form-item__content">
          <el-button @click="addShow = false">取 消</el-button>
          <el-button type="primary" @click="upRoles('upForm')">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rolesList: [],
      addShow: false,
      upShow: false,
      rolesShow: false,
      addForm: {
        roleName: "",
        roleDesc: "",
      },
      upForm: {},
      rules: {
        roleName: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
        roleDesc: [{ required: true, message: "请输入角色描述", trigger: "blur" }],
      },
      defaultProps: {
        children: "children",
        label: "authName",
      },
      data: "",
    };
  },
  methods: {
    lesShow(obj) {
      console.log(obj);
      //  this.$refs.tree.setCheckedNodes(obj)
      this.rolesShow = true;
    },
    addRoles(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http.post("roles", this.addForm).then((res) => {
            if (res.data.meta.status == 201) {
              this.$message({
                message: res.data.meta.msg,
                type: "success",
              });
              this.addShow = false;
              this.getRolesList();
            } else {
              this.$message.error(res.data.meta.msg);
            }
          });
        }
      });
    },
    upRoles(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http.put("roles/" + this.upForm.roleId, this.upForm).then((res) => {
            if (res.data.meta.status == 200) {
              this.$message({
                message: res.data.meta.msg,
                type: "success",
              });
              this.upShow = false;
              this.getRolesList();
            } else {
              this.$message.error(res.data.meta.msg);
            }
          });
        }
      });
    },
    getId(id) {
      this.$http.get("roles/" + id).then((res) => {
        if (res.data.meta.status == 200) {
          this.$message({
            message: res.data.meta.msg,
            type: "success",
          });
          this.upShow = true;
          this.upForm = res.data.data;
        } else {
          this.$message.error(res.data.meta.msg);
        }
      });
    },
    reUsers(id) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$http.delete("roles/" + id).then((res) => {
          if (res.data.meta.status == 200) {
            this.$message({
              message: res.data.meta.msg,
              type: "success",
            });
            this.getRolesList();
          } else {
            this.$message.error(res.data.meta.msg);
          }
        });
      });
    },
    getRolesList() {
      this.$http("roles").then((res) => {
        this.rolesList = res.data.data;
      });
    },
    rmTagRoles(roleId, rightId) {
      this.$confirm("此操作将取消权限, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$http.delete(`roles/${roleId}/rights/${rightId}`).then((res) => {
          if (res.data.meta.status == 200) {
            this.$message({
              message: res.data.meta.msg,
              type: "success",
            });
          } else {
            this.$message.error(res.data.meta.msg);
          }
        });
        let obj = this.rolesList.filter((ele) => ele.id == roleId)[0].children;
        obj.forEach((ele, in1) => {
          if (ele.id == rightId) {
            obj.splice(
              obj.findIndex((ele) => ele.id == rightId),
              1
            );
          } else {
            ele.children.forEach((ele, in2) => {
              if (ele.id == rightId) {
                obj[in1].children.splice(
                  obj[in1].children.findIndex((ele) => ele.id == rightId),
                  1
                );
              } else {
                ele.children.forEach((ele) => {
                  if (ele.id == rightId) {
                    obj[in1].children[in2].children.splice(
                      obj[in1].children[in2].children.findIndex(
                        (ele) => ele.id == rightId
                      ),
                      1
                    );
                  }
                });
              }
            });
          }
        });
      });
    },
  },
  mounted() {
    this.getRolesList();
    this.$http.get("rights/tree").then((res) => {
      this.data = res.data.data;
    });
  },
};
</script>

<style lang="scss">
.roles {
  .el-tag {
    margin: 5px;
  }
}
.el-form-item__content {
  display: flex;
  justify-content: flex-end;
}
</style>
